package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.SymbolConstant;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/4 21:11
 * @since v1.0
 */
@Service("workflowOperatorTextHandlerImpl")
public class WorkflowOperatorTextHandlerImpl implements WorkflowOperatorFieldHandler {

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.TEXT.getType(), FieldTypeEnum.TEXTAREA.getType());
    }


    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        Object value = workflowDataPojo.getValue();
        if (Objects.isNull(workflowDataPojo.getValue())) {
            value = data.get(fieldAttrEntity.getAttr());
        }
        if (Objects.isNull(value)) {
            return null;
        }
        if (String.valueOf(value).contains(SymbolConstant.HYPERTEXT_SPACE)) {
            // 替换掉空格
            return String.valueOf(value).replaceAll(SymbolConstant.HYPERTEXT_SPACE, " ");
        }
        return value;
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        return workflowOperationsPojo.getValue();
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data, Long formId, Integer businessType,  String corpid) throws XbbException {
        Object value = data.get(fieldAttrEntity.getAttr());
        if (Objects.isNull(value)) {
            return "\"\"";
        }
        return "\""+ value + "\"";
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        if (Objects.isNull(value)) {
            return "\"\"";
        }
        if (String.valueOf(value).contains(SymbolConstant.HYPERTEXT_SPACE)) {
            // 替换掉空格
            return String.valueOf(value).replaceAll(SymbolConstant.HYPERTEXT_SPACE, " ");
        }
        return value;
    }
}
